package com.beerq.view.me;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.beerq.R;
import com.beerq.util.AppConfig;
import com.beerq.util.BeerQPref;
import com.beerq.util.Constants;
import com.beerq.view.BaseActivity;
import com.beerq.view.ProductJsManager;
import com.beerq.view.WebViewActivity;
import com.beerq.view_manager.WeChatManager;

public class PersonalSetting extends BaseActivity {

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarType=1;
        super.onCreate(savedInstanceState);
        setMainContentView(R.layout.activity_personal_setting);
        mContext=this;
        setEvent();
        setBarTitle("个人设置");


        ((TextView)findViewById(R.id.txt_version)).setText(AppConfig.getAppInfo(mContext));


    }

    private void makeShareShow(){
        View view = LayoutInflater.from(mContext).inflate(R.layout.share_popwindow_view, null);
        final Dialog dialog = new Dialog(this, R.style.common_dialog);
        dialog.setContentView(view);
//        dialog.getWindow().setWindowAnimations(R.style.MyDialogAnimationStyle);
        dialog.show();

        // 设置相关位置，一定要在 show()之后
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);

        view.findViewById(R.id.lin_share_wechat_friend).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WeChatManager.wechatShare(mContext,1, "啤气","http://www.beerqi.com");
                dialog.dismiss();
            }
        });

        view.findViewById(R.id.lin_share_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WeChatManager.wechatShare(mContext,0, "啤气","http://www.beerqi.com");
                dialog.dismiss();
            }
        });

    }

    private void setEvent(){
        findViewById(R.id.lin_clear_cache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "缓存已清空", Toast.LENGTH_SHORT).show();
            }
        });




        //个人消息
        findViewById(R.id.rel_personal_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,PersonalInfo.class);
                startActivity(intent);
            }
        });

        //分享App
        findViewById(R.id.lin_share_app).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeShareShow();
            }
        });

        //关于我们
        findViewById(R.id.rel_about_us).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"/setview/about.html");
                startActivity(intent);
            }
        });

        //货品退换
        findViewById(R.id.rel_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"/setview/exchange.html");
                startActivity(intent);
            }
        });

        //常见问题
        findViewById(R.id.rel_common_problem).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"/setview/problem.html");
                startActivity(intent);
            }
        });

        //配送说明
        findViewById(R.id.rel_deliver_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"/setview/dispatch.html");
                startActivity(intent);
            }
        });

        //企业服务
        findViewById(R.id.rel_enterprise_server).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("barType","1");
                intent.putExtra("url", Constants.domain+"/setview/business.html");
                startActivity(intent);
            }
        });


        //退出登录
        findViewById(R.id.btn_login_out).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder=new AlertDialog.Builder(mContext).setTitle("提示").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        BeerQPref.getInstance(getApplicationContext()).setUserInfo("");
                        Constants.userInfo=null;
                        ProductJsManager.clearUserInfo(Constants.firstWebView);
                        finish();
                    }
                }).setTitle("你确定要退出登录吗？").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.show();



            }
        });
    }

}
